package com.example.guiguihomemedical.controller;

import com.example.guiguihomemedical.common.Result;
import com.example.guiguihomemedical.entity.Doctor;
import com.example.guiguihomemedical.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @PostMapping("/login")
    public Result login(@RequestBody Doctor doctor) {
        Doctor d = doctorService.login(doctor);
        if (d != null) {
            return Result.success(d);
        } else {
            return Result.error("用户名密码错误！");
        }
    }

    @PostMapping("/ChangePassword")
    public Result ChangePassword(@RequestParam(required = false, value = "newPassword") String newPassword, @RequestParam(required = false, value = "oldPassword") String oldPassword,@RequestParam(required = false, value = "doctorId") int doctorId) {
        Doctor d = doctorService.findByDoctorId(doctorId);

        if (d.getPassword().equals(oldPassword)) {
            doctorService.ChangePassword(newPassword,doctorId);
            return Result.success("修改成功！");
        } else {
            return Result.error("旧密码错误！");
        }
    }
}
